<?php
// 本类由系统自动生成，仅供测试用途
class FileAction extends CommonAction {

	public function upload(){
		import("ORG.Net.UploadFile");
		$upload = new UploadFile(); // 实例化上传类
		$upload->maxSize =200000000; // 设置附件上传大小
		$upload->allowExts = array('jpg','gif','png','jpeg');
		$upload->savePath = './Public/Uploads/';
		// 		echo '??'.$upload["name"];
		if(!$upload->upload()) { // 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{ // 上传成功获取上传文件信息
			$info = $upload->getUploadFileInfo();
			$attachement=M("attachment");
			$attachement->name=$info[0]["name"];
			$attachement->uuid=$info[0]["savename"];
			$attachement->path=$info[0]["savepath"];
			$attachement->pid=19;//暂时写死
			$id=$attachement->add();
			$result["id"]=$id;
			outJson($result);
				
				
		} // 保存表单数据包括附件数据
	}

	public function downLoad(){
		$id=I("id",0);
		$attachment=M("attachment")->where(" id=".$id )->find();
		$basename=$attachment["name"];
		$file =$attachment["path"].$attachment["uuid"];
		if (file_exists($file) == false) {
			die('文件不存在');
		}else{
			
			header('Content-type: image/jpeg');
			header("Content-Disposition: attachment; filename={$basename}");
			//在header确保前面没有任何输出
			@readfile($file);
			exit;
		}
	}

    //SAE
//    function upload()
// 	{
//        $loginName=I('loginName','');
//        $upload=new SaeStorage();
//        $name=$_FILES["Filedata"]["name"];
//        $basename=pathinfo($name,PATHINFO_EXTENSION);
//        $tmp_name= $_FILES['Filedata']['tmp_name'];
//        $filename=date('YmdHis')."_".rand(100,999).'.'.$basename;   //获取随机文件名
//        if(''!=$loginName){
//            $filename='IMG'.$loginName.$filename;
//        }
//        $attr = array();
//        $domain = 'woaiyiyi';
//        $s=$upload->upload($domain,$filename,$tmp_name,-1, $attr, false);
//        if($s){
//            $attachement=M("attachment");
//            $attachement->name=$name;
//            $attachement->uuid=$s;
//            $attachement->path="";
//            $attachement->pid=19;//暂时写死
//            $id=$attachement->add();
//            $result["id"]=$id;
//            outJson($result);
//        }else{
//            echo $fileName."上传失败！";
//        }
// 	}

}